package com.team.arms.political.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.arms.bingyiInfo.mapper.BingyiInfoMapper;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.political.domain.PoliReviewInfo;
import com.team.arms.political.domain.dto.Polidto;
import com.team.arms.political.domain.dto.Poliupdadto;
import com.team.arms.political.domain.vo.Polivo;
import com.team.arms.political.mapper.PoliReviewInfoMapper;
import com.team.arms.political.mapper.PoliupdateMapper;
import com.team.arms.political.service.PoliReviewInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author huazige
 * @Date 2022/4/29 22:09
 * @Version 1.0
 */
@RequestMapping("/political")
@RestController
@CrossOrigin
@Api(tags = "政治考核-待政审")
public class PoliticalController extends BaseController {


    @Autowired
    PoliReviewInfoService poliReviewInfoService;
    @Resource
    PoliReviewInfoMapper poliReviewInfoMapper;
    @Autowired
    PoliupdateMapper poliupdateMapper;

    /**
     * 查询所有待政审信息
     */
    @ApiOperation("获取所有信息")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/getpol")
    public AjaxResult selectpltc() {

       List<Polivo> list = poliReviewInfoMapper.selectpltc();
//

        if (list!=null){
            return AjaxResult.success("查询成功",list) ;
        }
        else {
            return AjaxResult.error("查询失败");
        }


    }
   /* @ApiOperation("新增")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @PostMapping(value = "/insertpol")
    public AjaxResult insertpol(@RequestBody
                                Polivo polivo
                                ) {
        Polivo polivo1 = poliReviewInfoMapper.insertpol(polivo);
//

        if (polivo1!=null){
            return AjaxResult.success("新增成功",polivo1) ;
        }
        else {
            return AjaxResult.error("新增失败");
        }


    }*/


 

    @ApiOperation(value = "下发走访任务")
    @PostMapping ("/send")
    /*三张表的数据，删除的时候，直接这三个都删除*/
    public AjaxResult send( @RequestParam("poliInfoId")String id) {
        PoliReviewInfo person = poliReviewInfoService.getById(id);
        person.setPoliCarefulState("政审中");
        QueryWrapper<PoliReviewInfo> wrapper = new QueryWrapper();
        wrapper.eq("poli_info_id",id);
        boolean b =   poliReviewInfoService.update(person,wrapper);//前新后旧
        return b ? AjaxResult.success("下发成功",person) : AjaxResult.error("下发失败");
    }
    @Autowired
    BingyiInfoMapper bingyiInfoMapper;

    @ApiOperation(value = "姓名查找")
    @PostMapping ("/find")
    //
//    三张表的数据，删除的时候，直接这三个都删除
    public AjaxResult find( @RequestParam("name")String name) {
       //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        QueryWrapper<PoliReviewInfo> wrapper = new QueryWrapper();
        wrapper.eq("name",name);
        List<Polivo> list = poliReviewInfoMapper.selectbyname(name);
        if (list.size()>0){
            return AjaxResult.success("姓名查找成功",list);
        }else {
            return    AjaxResult.success("没有找到该人待政审信息");
        }

    }

    @ApiOperation(value = "新增政审信息(刚体检过的人进入待政审中)")
    @PostMapping ("/add")
    //
//    三张表的数据，删除的时候，直接这三个都删除
    //新增的话指的是从前面传过来
    public AjaxResult add( @RequestParam("poliInfoId")String id){
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据
        PoliReviewInfo person = poliReviewInfoService.getById(id);

        person.setPoliCarefulState("待政审");
        QueryWrapper<PoliReviewInfo> wrapper = new QueryWrapper();
        wrapper.eq("poli_info_id",id);
        boolean b =   poliReviewInfoService.update(person,wrapper);
        return b ? AjaxResult.success("新增成功",person) : AjaxResult.error("新增失败");


    }


    //是吧这个人的待政审状态修改，但是这个人的应征信息，体检一些信息应该都要存在的,只需要在这个页面不显示就行
    @ApiOperation("删除待政审状态(应征信息保留)")
    @DeleteMapping("/delete")
    public AjaxResult deleteInformation(@RequestParam("poliInfoId") String id){

        PoliReviewInfo person = poliReviewInfoService.getById(id);
        person.setPoliCarefulState("");
        QueryWrapper<PoliReviewInfo> wrapper = new QueryWrapper();
        wrapper.eq("poli_info_id",id);
        boolean b =   poliReviewInfoService.update(person,wrapper);
        return b ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");

    }
    @ApiOperation("批量删除政审信息(应征信息保留)")
    @DeleteMapping("/deletelist")
    public AjaxResult deleteInformation(@RequestParam("poliInfoId") List<String> deletelist){
        List<PoliReviewInfo> poliReviewInfos = poliReviewInfoMapper.selectBatchIds(deletelist);
        for (PoliReviewInfo poliReviewInfo:poliReviewInfos
             ) {
            poliReviewInfo.setPoliCarefulState("");
        }
        QueryWrapper<PoliReviewInfo> wrapper = new QueryWrapper();
        for (String id:deletelist
             ) {
            wrapper.eq("poli_info_id",id);
        }

        return toAjax(poliReviewInfoService.removeByIds(deletelist));


    }

    @ApiOperation(value = "高级检索")
      @PostMapping("/search")
      /*三张表的数据，删除的时候，直接这三个都删除*/
      public AjaxResult search(
            @RequestBody Polidto Polidto
    ) {
        /*name,sex,nation,year,batch,login_id,household_type,cultureral_program,
            registration_time,political_check_results*/
        String sex = Polidto.getSex();
        String batch = Polidto.getBatch();

        String householdType = Polidto.getHouseholdType();
        String schoolName = Polidto.getSchoolName();
        String culture_program = Polidto.getCultureProgram();
        String bingyiPlace = Polidto.getBingyiPlace();
        if (householdType=="所有")
        {
            householdType=null;
        }

        List<Polivo> list = poliReviewInfoMapper.listByType(sex, batch, householdType,
                schoolName, culture_program,bingyiPlace);
        if (list.size()>0){
            return AjaxResult.success("高级检索成功",list);
        }else {
            return    AjaxResult.success("未找到对应检索条件的信息");
        }

    }
    /**
     * 导出政审信息excel
        */
    //根据info信息导出?list
  /*  @ApiOperation("导出政审信息")
    @PostMapping("/exportexc")
    public void export(HttpServletResponse response, Polivo polivo)
    {
        List<Polivo> list = poliReviewInfoMapper.selectpltc();
        ExcelUtil<Polivo> util = new ExcelUtil<Polivo>(Polivo.class);
        util.exportExcel(response, list, "兵役信息数据");

    }*/


   /* @ApiOperation("学历认证-获取学历信息")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/getcul")
    public AjaxResult select() {
        List<Culvo> list = poliReviewInfoService.selectcul();
//

        if (list!=null){
            return AjaxResult.success("查询成功",list) ;
        }
        else {
            return AjaxResult.error("查询失败");
        }


    }
    @ApiOperation("学历认证-搜索")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @PostMapping(value = "/getcul")
    public AjaxResult selectcul(     @PathVariable("current")Integer current,
                                     @PathVariable("size")Integer size,
                                     @RequestBody Culvo culvo) {
        String name = culvo.getName();
        String idCard = culvo.getIdCard();
        String year = culvo.getYear();
        String batch = culvo.getBatch();
        String poliCarefulState = culvo.getPoliCarefulState();


        List<Culvo> list = poliReviewInfoMapper.listbycul(name, idCard,  year, batch,poliCarefulState);
//

        Page page = PageUtil.list2Page(list, current, size);
//        AjaxResult.success("搜索成功","readypoliticalvos") : AjaxResult.error("搜索异常");
        return page !=null ? AjaxResult.success("分页成功",page): AjaxResult.error("分页失败");


    }*/
    @ApiOperation("导出所有待政审信息")
    @GetMapping("/exportexc")
    public void export(HttpServletResponse response)
    {
        //能不能根据对方打印的次数，不同次调用的时候，添加这个不同的后缀显示
        //redis缓存进行记录调用的次数.
        List<Polivo> polivoList = poliReviewInfoMapper.selectpltc();
      poliReviewInfoService.exportpoli(response,polivoList,Polivo.class,"待政审信息表");
       /* try {
            EasyExcel.write(response.getOutputStream(), VoluntaryInformation.class).sheet("待政审信息")
                    .doWrite(polivoList);
        } catch (Exception e) {
            e.printStackTrace();
        }*/

    }

    @ApiOperation("修改待政审信息")
    @PutMapping("/update")
    public AjaxResult updateInformation(
            @RequestParam(value = "poliinfoid")String poliinfoid,
            @RequestBody Poliupdadto poliupdadto){

        Boolean b = poliupdateMapper.updateBypoliinfoid(poliinfoid, poliupdadto);

        return b ? AjaxResult.success("更新成功",poliupdadto) : AjaxResult.error("更新失败");

    }


}
